from PIL import Image,ImageDraw,ImageFont
import pytesseract

def get_wh():
	'''获取长宽'''
	img1 = Image.open('img_part.jpg')
    print(img1.size)

def get_bin_table(threshold=140):
    """
    获取灰度转二值的映射table
    :param threshold:
    :return:
    """
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
 
    return table

def get_binary_image():
	'''图片变黑白'''
	image = Image.open('src.png')
	imgry = image.convert('L')  # 转化为灰度图
	imgry.save('dst.png')

def get_binary_image():
	'''图片灰度化加二值化'''
	image = Image.open('cropped_image.png')
	imgry = image.convert('L')  # 转化为灰度图
	table = get_bin_table()
	out = imgry.point(table, '1')
	out.save('cropped_image1.png')

def crop():
	'''图片剪切'''
	img=Image.open('SecurePwdImage.png')
	cropped_image = img.crop((8, 8, 38, 38))
	cropped_image.save("cropped_image.png")

def paste():
	'''图片拼接  crop为一个Image对象'''
	img_new = Image.new('RGBA',(60,30))
	img_new.paste(crop1,(0,0,30,30))
	img_new.paste(crop2,(30,0,60,30))
	img_new.save("cropped_image11.png")

def img_recognition():
	'''图片识别 字符串'''
	img=Image.open('SecurePwdImage.png')
	result = pytesseract.image_to_string(img,config="-psm 7")

def draw_circle():
	'''画圆'''
	img = Image.open(fp)
	draw = ImageDraw.Draw(img)
	draw.ellipse((x1,y1,x2,y2), fill = (255, 0, 0))
	img.show()

def draw_font():
	'''写字'''
	img = Image.open(fp)
	draw = ImageDraw.Draw(img)
	#draw.text((40, 100), '1',font=ImageFont.truetype('C:/windows/fonts/Dengl.ttf', 20), fill="#ff0000")
	draw.text((40, 100), '1', fill = (255, 0 ,0))
	img.show()